﻿using System;
using BenjaminSchroeter.Dsl.CompartmentMapping;
using Microsoft.VisualStudio.Modeling;

namespace BenjaminSchroeter.AdvancedCompartmentMapping
{
    partial class BaseDataReferencesTargetBaseDatumBuilderInstance
    {
        protected override ElementLink CreateElementLink(BaseData source, SelectedCompartmentPartType sourcePartType,
                                                         Entry sourceEntry, BaseData target,
                                                         SelectedCompartmentPartType targetPartType, Entry targetEntry)
        {
            BaseDataReferencesTargetBaseDatum result = new BaseDataReferencesTargetBaseDatum(source,target);
            
            // an entry can be null if
            //   - the header of the CompartmentShape is selected (but the head is not allows, see the xml file)
            //   - the GeometryShape is selected
            result.fromEntry = sourceEntry == null ? Guid.Empty : sourceEntry.Guid;
            result.toEntry = targetEntry == null ? Guid.Empty : targetEntry.Guid;
            return result;
        }

        public override bool IsEntryConnectionSource(Entry entry, BaseDataReferencesTargetBaseDatum connection)
        {
            if (entry == null)
                return connection.fromEntry.Equals(Guid.Empty);

            return connection.fromEntry.Equals(entry.Guid);
        }

        public override bool IsEntryConnectionTarget(Entry entry, BaseDataReferencesTargetBaseDatum connection)
        {
            if (entry == null)
                return connection.toEntry.Equals(Guid.Empty);

            return connection.toEntry.Equals(entry.Guid);
        }

        protected override bool CanAcceptAsCompartmentSource(BaseData candidate, SelectedCompartmentPartType partType,
                                                     Entry candidateEntry)
        {
            // valid sources are:
            //  - SimpleData if Kind == Output
            //  - the Output class as entry

            if (candidate is SimpleData)
                return (candidate as SimpleData).Kind == SimpleDataKind.Output;

            return candidateEntry is Output;
        }

        protected override bool CanAcceptAsCompartmentTarget(BaseData candidate, SelectedCompartmentPartType partType,
                                                             Entry candidateEntry)
        {
            // valid targets are:
            //  - SimpleData if Kind == Input
            //  - the Input class as entry

            if (candidate is SimpleData)
                return (candidate as SimpleData).Kind == SimpleDataKind.Input;

            return candidateEntry is Input;
        }
    }
}
